﻿using System;
using System.Data;
using System.Collections.Generic;
using Things.Common;
using Things.Model;
using Things.DALFactory;
using Things.IDAL;
namespace Things.BLL
{
	/// <summary>
	/// Goods
	/// </summary>
	public partial class Goods
	{
		private readonly IGoods dal=DataAccess.CreateGoods();
		public Goods()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid GoodsID)
		{
			return dal.Exists(GoodsID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Things.Model.Goods model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Things.Model.Goods model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid GoodsID)
		{
			
			return dal.Delete(GoodsID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string GoodsIDlist )
		{
			return dal.DeleteList(GoodsIDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Things.Model.Goods GetModel(Guid GoodsID)
		{
			
			return dal.GetModel(GoodsID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Things.Model.Goods GetModelByCache(Guid GoodsID)
		{
			
			string CacheKey = "GoodsModel-" + GoodsID;
			object objModel = Things.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(GoodsID);
					if (objModel != null)
					{
						int ModelCache = Things.Common.ConfigHelper.GetConfigInt("ModelCache");
						Things.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Things.Model.Goods)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}

        public DataSet GetList()
        {
            return dal.GetList();
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Things.Model.Goods> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Things.Model.Goods> DataTableToList(DataTable dt)
		{
			List<Things.Model.Goods> modelList = new List<Things.Model.Goods>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Things.Model.Goods model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Things.Model.Goods();
					if(dt.Rows[n]["GoodsID"]!=null && dt.Rows[n]["GoodsID"].ToString()!="")
					{
						model.GoodsID=new Guid(dt.Rows[n]["GoodsID"].ToString());
					}
					if(dt.Rows[n]["ProductID"]!=null && dt.Rows[n]["ProductID"].ToString()!="")
					{
						model.ProductID=new Guid(dt.Rows[n]["ProductID"].ToString());
					}
					if(dt.Rows[n]["Description"]!=null && dt.Rows[n]["Description"].ToString()!="")
					{
					model.Description=dt.Rows[n]["Description"].ToString();
					}
					if(dt.Rows[n]["ShelfLife"]!=null && dt.Rows[n]["ShelfLife"].ToString()!="")
					{
					model.ShelfLife=dt.Rows[n]["ShelfLife"].ToString();
					}
					if(dt.Rows[n]["Type"]!=null && dt.Rows[n]["Type"].ToString()!="")
					{
					model.Type=dt.Rows[n]["Type"].ToString();
					}
					if(dt.Rows[n]["Price"]!=null && dt.Rows[n]["Price"].ToString()!="")
					{
						model.Price=decimal.Parse(dt.Rows[n]["Price"].ToString());
					}
					if(dt.Rows[n]["CropsName"]!=null && dt.Rows[n]["CropsName"].ToString()!="")
					{
					model.CropsName=dt.Rows[n]["CropsName"].ToString();
					}
					if(dt.Rows[n]["CropsAddress"]!=null && dt.Rows[n]["CropsAddress"].ToString()!="")
					{
					model.CropsAddress=dt.Rows[n]["CropsAddress"].ToString();
					}
					if(dt.Rows[n]["Count"]!=null && dt.Rows[n]["Count"].ToString()!="")
					{
						model.Count=int.Parse(dt.Rows[n]["Count"].ToString());
					}
					if(dt.Rows[n]["IsDeleted"]!=null && dt.Rows[n]["IsDeleted"].ToString()!="")
					{
						if((dt.Rows[n]["IsDeleted"].ToString()=="1")||(dt.Rows[n]["IsDeleted"].ToString().ToLower()=="true"))
						{
						model.IsDeleted=true;
						}
						else
						{
							model.IsDeleted=false;
						}
					}
					if(dt.Rows[n]["GoodsName"]!=null && dt.Rows[n]["GoodsName"].ToString()!="")
					{
					model.GoodsName=dt.Rows[n]["GoodsName"].ToString();
					}
					if(dt.Rows[n]["GoodsBrand"]!=null && dt.Rows[n]["GoodsBrand"].ToString()!="")
					{
					model.GoodsBrand=dt.Rows[n]["GoodsBrand"].ToString();
					}
					if(dt.Rows[n]["GoodsWeight"]!=null && dt.Rows[n]["GoodsWeight"].ToString()!="")
					{
					model.GoodsWeight=dt.Rows[n]["GoodsWeight"].ToString();
					}
					if(dt.Rows[n]["GoodsPackage"]!=null && dt.Rows[n]["GoodsPackage"].ToString()!="")
					{
					model.GoodsPackage=dt.Rows[n]["GoodsPackage"].ToString();
					}
					if(dt.Rows[n]["GoodsCategoryID"]!=null && dt.Rows[n]["GoodsCategoryID"].ToString()!="")
					{
						model.GoodsCategoryID=new Guid(dt.Rows[n]["GoodsCategoryID"].ToString());
					}
					if(dt.Rows[n]["GoodsImage"]!=null && dt.Rows[n]["GoodsImage"].ToString()!="")
					{
						model.GoodsImage=(byte[])dt.Rows[n]["GoodsImage"];
					}
					if(dt.Rows[n]["GoodsImageURL"]!=null && dt.Rows[n]["GoodsImageURL"].ToString()!="")
					{
					model.GoodsImageURL=dt.Rows[n]["GoodsImageURL"].ToString();
					}

                    if (dt.Rows[n]["GoodsNotice"] != null)
                    {
                        model.GoodsNotice = dt.Rows[n]["GoodsNotice"].ToString();
                    }
                    if (dt.Rows[n]["ShengChanPiCi"] != null)
                    {
                        model.ShengChanPiCi = dt.Rows[n]["ShengChanPiCi"].ToString();
                    }
                    if (dt.Rows[n]["ChuCangFangFa"] != null)
                    {
                        model.ChuCangFangFa = dt.Rows[n]["ChuCangFangFa"].ToString();
                    }
                    if (dt.Rows[n]["ShengChanXuKeZhengBianHao"] != null)
                    {
                        model.ShengChanXuKeZhengBianHao = dt.Rows[n]["ShengChanXuKeZhengBianHao"].ToString();
                    }
                    if (dt.Rows[n]["ChanPinBiaoZhunHao"] != null)
                    {
                        model.ChanPinBiaoZhunHao = dt.Rows[n]["ChanPinBiaoZhunHao"].ToString();
                    }
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}


        public DataSet GetTrade(string tradeCode)
        {
            return dal.GetTrade(tradeCode);
        }

        /// <summary>
        /// 首页数据列表
        /// </summary>
        /// <param name="goodsCategoryName"></param>
        /// <param name="priceType"></param>
        /// <param name="filedOrder"></param>
        /// <returns></returns>
        public DataSet GetList(string goodsCategoryName, string priceType, string filedOrder)
        {
            return dal.GetList(goodsCategoryName, priceType, filedOrder);
        }
		#endregion  Method
	}
}

